home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / files / standard file / standardgetfolder / fspcompat.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  25.4 KB  |  931 lines

  1. /*
  2.     File:        FSpCompat.c
  3.     
  4.     Description:FSSpec compatibility functions.
  5.  
  6.     Author:        JL
  7.  
  8.     Copyright:     Copyright: © 1992-1999 by Apple Computer, Inc.
  9.                 all rights reserved.
  10.     
  11.     Disclaimer:    You may incorporate this sample code into your applications without
  12.                 restriction, though the sample code has been provided "AS IS" and the
  13.                 responsibility for its operation is 100% yours.  However, what you are
  14.                 not permitted to do is to redistribute the source as "DSC Sample Code"
  15.                 after having made changes. If you're going to re-distribute the source,
  16.                 we require that you make it clear in the source that the code was
  17.                 descended from Apple Sample Code, but that you've made changes.
  18.     
  19.     Change History (most recent first):
  20.                 6/25/99    Updated for Metrowerks Codewarror Pro 2.1(KG)
  21.  
  22. */
  23.  
  24. /*
  25. **    If building application 68K code, set GENERATENODATA to 0 for faster code.
  26. **    If building stand-alone 68K code, set GENERATENODATA to 1 so globals
  27. **        (static variables) are not used.
  28. */
  29. #ifndef GENERATENODATA
  30. #define GENERATENODATA 0
  31. #endif
  32.  
  33. #include <Types.h>
  34. #include <Errors.h>
  35. #include <LowMem.h>
  36. #include <Gestalt.h>
  37. #include <Resources.h>
  38. #include <Script.h>
  39.  
  40. #define    __COMPILINGMOREFILES
  41.  
  42. #include "MoreFilesExtras.h"
  43. #include "FSpCompat.h"
  44.  
  45. /*****************************************************************************/
  46.  
  47. /* local constants */
  48.  
  49. enum {
  50.     gestaltBugFixAttrsTwo                    = 'bugy',
  51.     gestaltFSpExchangeFilesCompatibilityFix    = 26,
  52.     gestaltBugFixAttrsThree                    = 'bugx',
  53.     gestaltFSpCreateScriptSupportFix        = 1
  54. };
  55.  
  56. /*****************************************************************************/
  57.  
  58. /* static prototypes */
  59.  
  60.  
  61. #if !__MACOSSEVENORLATER
  62. static    Boolean    FSHasFSSpecCalls(void);
  63.  
  64. static    Boolean    QTHasFSSpecCalls(void);
  65. #endif    /* !__MACOSSEVENORLATER */
  66.  
  67. #if !__MACOSSEVENFIVEORLATER
  68. static    Boolean    HasFSpExchangeFilesCompatibilityFix(void);
  69.  
  70. static    OSErr    GenerateUniqueName(short volume,
  71.                                    long *startSeed,
  72.                                    long dir1,
  73.                                    long dir2,
  74.                                    StringPtr uniqueName);
  75. #endif    /* !__MACOSSEVENFIVEORLATER */
  76.  
  77. #if !__MACOSSEVENFIVEONEORLATER
  78. static    Boolean    HasFSpCreateScriptSupportFix(void);
  79. #endif    /* !__MACOSSEVENFIVEONEORLATER */
  80.  
  81. /*****************************************************************************/
  82.  
  83. /* FSHasFSSpecCalls returns true if the file system provides FSSpec calls. */
  84.  
  85. #if !__MACOSSEVENORLATER
  86. static    Boolean    FSHasFSSpecCalls(void)
  87. {
  88.     long            response;
  89. #if !GENERATENODATA
  90.     static Boolean    tested = false;
  91.     static Boolean    result = false;
  92. #else
  93.     Boolean    result = false;
  94. #endif
  95.     
  96. #if !GENERATENODATA
  97.     if ( !tested )
  98.     {
  99.         tested = true;
  100. #endif
  101.         if ( Gestalt(gestaltFSAttr, &response) == noErr )
  102.         {
  103.             result = ((response & (1L << gestaltHasFSSpecCalls)) != 0);
  104.         }
  105. #if !GENERATENODATA
  106.     }
  107. #endif
  108.     return ( result );
  109. }
  110. #endif    /* !__MACOSSEVENORLATER */
  111.  
  112. /*****************************************************************************/
  113.  
  114. /* QTHasFSSpecCalls returns true if QuickTime provides FSSpec calls */
  115. /* except for FSpExchangeFiles. */
  116.  
  117. #if !__MACOSSEVENORLATER
  118. static    Boolean    QTHasFSSpecCalls(void)
  119. {
  120.     long            response;
  121. #if !GENERATENODATA
  122.     static Boolean    tested = false;
  123.     static Boolean    result = false;
  124. #else
  125.     Boolean    result = false;
  126. #endif
  127.     
  128. #if !GENERATENODATA
  129.     if ( !tested )
  130.     {
  131.         tested = true;
  132. #endif
  133.         result = (Gestalt(gestaltQuickTimeVersion, &response) == noErr);
  134. #if !GENERATENODATA
  135.     }
  136. #endif
  137.     return ( result );
  138. }
  139. #endif    /* !__MACOSSEVENORLATER */
  140.  
  141. /*****************************************************************************/
  142.  
  143. /* HasFSpExchangeFilesCompatibilityFix returns true if FSpExchangeFiles */
  144. /* compatibility code has been fixed in system software. */
  145. /* This was fixed by System Update 3.0, so if SystemSevenFiveOrLater */
  146. /* is true, then we know the fix is in. */
  147.  
  148. #if !__MACOSSEVENFIVEORLATER
  149. static    Boolean    HasFSpExchangeFilesCompatibilityFix(void)
  150. {
  151.     long            response;
  152. #if !GENERATENODATA
  153.     static Boolean    tested = false;
  154.     static Boolean    result = false;
  155. #else    /* !GENERATENODATA */
  156.     Boolean    result = false;
  157. #endif    /* !GENERATENODATA */
  158.     
  159. #if !GENERATENODATA
  160.     if ( !tested )
  161.     {
  162.         tested = true;
  163. #endif    /* !GENERATENODATA */
  164.         if ( Gestalt(gestaltBugFixAttrsTwo, &response) == noErr )
  165.         {
  166.             result = ((response & (1L << gestaltFSpExchangeFilesCompatibilityFix)) != 0);
  167.         }
  168. #if !GENERATENODATA
  169.     }
  170. #endif    /* !GENERATENODATA */
  171.     return ( result );
  172. }
  173. #endif    /* !__MACOSSEVENFIVEORLATER */
  174.  
  175. /*****************************************************************************/
  176.  
  177. /* HasFSpCreateScriptSupportFix returns true if FSpCreate and */
  178. /* FSpCreateResFile have been fixed in system software to correctly set */
  179. /* the scriptCode in the volume's catalog. */
  180. /* This was fixed by System 7.5 Update 1.0 */
  181.  
  182. #if !__MACOSSEVENFIVEONEORLATER
  183. static    Boolean    HasFSpCreateScriptSupportFix(void)
  184. {
  185.     long            response;
  186. #if !GENERATENODATA
  187.     static Boolean    tested = false;
  188.     static Boolean    result = false;
  189. #else
  190.     Boolean    result = false;
  191. #endif    /* !GENERATENODATA */
  192.     
  193. #if !GENERATENODATA
  194.     if ( !tested )
  195.     {
  196.         tested = true;
  197. #endif    /* !GENERATENODATA */
  198.         if ( Gestalt(gestaltBugFixAttrsThree, &response) == noErr )
  199.         {
  200.             result = ((response & (1L << gestaltFSpCreateScriptSupportFix)) != 0);
  201.         }
  202. #if !GENERATENODATA
  203.     }
  204. #endif    /* !GENERATENODATA */
  205.     return ( result );
  206. }
  207. #endif    /* !__MACOSSEVENFIVEONEORLATER */
  208.  
  209. /*****************************************************************************/
  210.  
  211. /*
  212. **    File Manager FSp calls
  213. */
  214.  
  215. /*****************************************************************************/
  216.  
  217. pascal    OSErr    FSMakeFSSpecCompat(short vRefNum,
  218.                                    long dirID,
  219.                                    ConstStr255Param fileName,
  220.                                    FSSpec *spec)
  221. {
  222.     OSErr    result;
  223.     
  224. #if !__MACOSSEVENORLATER
  225.     if ( !FSHasFSSpecCalls() && !QTHasFSSpecCalls() )
  226.     {
  227.         Boolean    isDirectory;
  228.         
  229.         result = GetObjectLocation(vRefNum, dirID, fileName,
  230.                                     &(spec->vRefNum), &(spec->parID), spec->name,
  231.                                     &isDirectory);
  232.     }
  233.     else
  234. #endif    /* !__MACOSSEVENORLATER */
  235.     {
  236.         /* Let the file system create the FSSpec if it can since it does the job */
  237.         /* much more efficiently than I can. */
  238.         result = FSMakeFSSpec(vRefNum, dirID, fileName, spec);
  239.  
  240.         /* Fix a bug in Macintosh PC Exchange's MakeFSSpec code where 0 is */
  241.         /* returned in the parID field when making an FSSpec to the volume's */
  242.         /* root directory by passing a full pathname in MakeFSSpec's */
  243.         /* fileName parameter. Fixed in Mac OS 8.1 */
  244.         if ( (result == noErr) && (spec->parID == 0) )
  245.             spec->parID = fsRtParID;
  246.     }
  247.     return ( result );
  248. }
  249.  
  250. /*****************************************************************************/
  251.  
  252. pascal    OSErr    FSpOpenDFCompat(const FSSpec *spec,
  253.                                 char permission,
  254.                                 short *refNum)
  255. {
  256. #if !__MACOSSEVENORLATER
  257.     if ( !FSHasFSSpecCalls() && !QTHasFSSpecCalls() )
  258.     {
  259.         OSErr            result;
  260.         HParamBlockRec    pb;
  261.         
  262.         pb.ioParam.ioVRefNum = spec->vRefNum;
  263.         pb.fileParam.ioDirID = spec->parID;
  264.         pb.ioParam.ioNamePtr = (StringPtr) &(spec->name);
  265.         pb.ioParam.ioVersNum = 0;
  266.         pb.ioParam.ioPermssn = permission;
  267.         pb.ioParam.ioMisc = NULL;
  268.         result = PBHOpenSync(&pb);    /* OpenDF not supported by System 6, so use Open */
  269.         *refNum = pb.ioParam.ioRefNum;
  270.         return ( result );
  271.     }
  272.     else
  273. #endif    /* !__MACOSSEVENORLATER */
  274.     {
  275.         return ( FSpOpenDF(spec, permission, refNum) );
  276.     }
  277. }
  278.  
  279. /*****************************************************************************/
  280.  
  281. pascal    OSErr    FSpOpenRFCompat(const FSSpec *spec,
  282.                                 char permission,
  283.                                 short *refNum)
  284. {
  285. #if !__MACOSSEVENORLATER
  286.     if ( !FSHasFSSpecCalls() && !QTHasFSSpecCalls() )
  287.     {
  288.         OSErr            result;
  289.         HParamBlockRec    pb;
  290.         
  291.         pb.ioParam.ioVRefNum = spec->vRefNum;
  292.         pb.fileParam.ioDirID = spec->parID;
  293.         pb.ioParam.ioNamePtr = (StringPtr) &(spec->name);
  294.         pb.ioParam.ioVersNum = 0;
  295.         pb.ioParam.ioPermssn = permission;
  296.         pb.ioParam.ioMisc = NULL;
  297.         result = PBHOpenRFSync(&pb);
  298.         *refNum = pb.ioParam.ioRefNum;
  299.         return ( result );
  300.     }
  301.     else
  302. #endif    /* !__MACOSSEVENORLATER */
  303.     {
  304.         return ( FSpOpenRF(spec, permission, refNum) );
  305.     }
  306. }
  307.  
  308. /*****************************************************************************/
  309.  
  310. pascal    OSErr    FSpCreateCompat(const FSSpec *spec,
  311.                                 OSType creator,
  312.                                 OSType fileType,
  313.                                 ScriptCode scriptTag)
  314. {
  315. #if !__MACOSSEVENFIVEONEORLATER
  316.     OSErr            result;
  317.     UniversalFMPB    pb;
  318.  
  319.     
  320.     if (
  321. #if !__MACOSSEVENORLATER
  322.          (!FSHasFSSpecCalls() && !QTHasFSSpecCalls()) ||
  323. #endif    /* !__MACOSSEVENORLATER */
  324.          !HasFSpCreateScriptSupportFix() )
  325.     {
  326.         /*    If FSpCreate isn't called, this code will be executed */
  327.         pb.hPB.fileParam.ioVRefNum = spec->vRefNum;
  328.         pb.hPB.fileParam.ioDirID = spec->parID;
  329.         pb.hPB.fileParam.ioNamePtr = (StringPtr) &(spec->name);
  330.         pb.hPB.fileParam.ioFVersNum = 0;
  331.         result = PBHCreateSync(&(pb.hPB));
  332.         if ( result == noErr )
  333.         {
  334.             /* get info on created item */
  335.             pb.ciPB.hFileInfo.ioFDirIndex = 0;
  336.             result = PBGetCatInfoSync(&(pb.ciPB));
  337.             if ( result == noErr )
  338.             {
  339.                 /* Set fdScript in FXInfo */
  340.                 /* The negative script constants (smSystemScript, smCurrentScript, and smAllScripts) */
  341.                 /* don't make sense on disk, so only use scriptTag if scriptTag >= smRoman */
  342.                 /* (smRoman is 0). fdScript is valid if high bit is set (see IM-6, page 9-38) */
  343.                 pb.ciPB.hFileInfo.ioFlXFndrInfo.fdScript = (scriptTag >= smRoman) ?
  344.                                                             ((char)scriptTag | (char)0x80) :
  345.                                                             (smRoman);
  346.                 /* Set creator/fileType */
  347.                 pb.ciPB.hFileInfo.ioFlFndrInfo.fdCreator = creator;
  348.                 pb.ciPB.hFileInfo.ioFlFndrInfo.fdType = fileType;
  349.                 /* Restore ioDirID field in pb which was changed by PBGetCatInfo */
  350.                 pb.ciPB.hFileInfo.ioDirID = spec->parID;
  351.                 result = PBSetCatInfoSync(&(pb.ciPB));
  352.             }
  353.         }
  354.         return ( result );
  355.     }
  356.     else
  357. #endif    /* !__MACOSSEVENFIVEONEORLATER */
  358.     {
  359.         return ( FSpCreate(spec, creator, fileType, scriptTag) );
  360.     }
  361. }
  362.  
  363. /*****************************************************************************/
  364.  
  365. pascal    OSErr    FSpDirCreateCompat(const FSSpec *spec,
  366.                                    ScriptCode scriptTag,
  367.                                    long *createdDirID)
  368. {
  369. #if !__MACOSSEVENORLATER
  370.     if ( !FSHasFSSpecCalls() && !QTHasFSSpecCalls() )
  371.     {
  372.         OSErr            result;
  373.         UniversalFMPB    pb;
  374.         
  375.         pb.hPB.fileParam.ioVRefNum = spec->vRefNum;
  376.         pb.hPB.fileParam.ioDirID = spec->parID;
  377.         pb.hPB.fileParam.ioNamePtr = (StringPtr) &(spec->name);
  378.         result = PBDirCreateSync(&(pb.hPB));
  379.         *createdDirID = pb.hPB.fileParam.ioDirID;
  380.         if ( result == noErr )
  381.         {
  382.             /* get info on created item */
  383.             pb.ciPB.dirInfo.ioFDirIndex = 0;
  384.             pb.ciPB.dirInfo.ioDrDirID = spec->parID;
  385.             result = PBGetCatInfoSync(&(pb.ciPB));
  386.             if ( result == noErr )
  387.             {
  388.                 /* Set frScript in DXInfo */
  389.                 /* The negative script constants (smSystemScript, smCurrentScript, and smAllScripts) */
  390.                 /* don't make sense on disk, so only use scriptTag if scriptTag >= smRoman */
  391.                 /* (smRoman is 0). frScript is valid if high bit is set (see IM-6, page 9-38) */
  392.                 pb.ciPB.dirInfo.ioDrFndrInfo.frScript = (scriptTag >= smRoman) ?
  393.                                                             ((char)scriptTag | (char)0x80) :
  394.                                                             (smRoman);
  395.                 /* Restore ioDirID field in pb which was changed by PBGetCatInfo */
  396.                 pb.ciPB.dirInfo.ioDrDirID = spec->parID;            
  397.                 result = PBSetCatInfoSync(&(pb.ciPB));
  398.             }
  399.         }
  400.         return ( result );
  401.     }
  402.     else
  403. #endif    /* !__MACOSSEVENORLATER */
  404.     {
  405.         return ( FSpDirCreate(spec, scriptTag, createdDirID) );
  406.     }
  407. }
  408.  
  409. /*****************************************************************************/
  410.  
  411. pascal    OSErr    FSpDeleteCompat(const FSSpec *spec)
  412. {
  413. #if !__MACOSSEVENORLATER
  414.     if ( !FSHasFSSpecCalls() && !QTHasFSSpecCalls() )
  415.     {
  416.         HParamBlockRec    pb;
  417.         
  418.         pb.ioParam.ioVRefNum = spec->vRefNum;
  419.         pb.fileParam.ioDirID = spec->parID;
  420.         pb.ioParam.ioNamePtr = (StringPtr) &(spec->name);
  421.         pb.ioParam.ioVersNum = 0;
  422.         return ( PBHDeleteSync(&pb) );
  423.     }
  424.     else
  425. #endif    /* !__MACOSSEVENORLATER */
  426.     {
  427.         return ( FSpDelete(spec) );
  428.     }
  429. }
  430.  
  431. /*****************************************************************************/
  432.  
  433. pascal    OSErr    FSpGetFInfoCompat(const FSSpec *spec,
  434.                                   FInfo *fndrInfo)
  435. {
  436. #if !__MACOSSEVENORLATER
  437.     if ( !FSHasFSSpecCalls() && !QTHasFSSpecCalls() )
  438.     {
  439.         OSErr            result;
  440.         HParamBlockRec    pb;
  441.         
  442.         pb.fileParam.ioVRefNum = spec->vRefNum;
  443.         pb.fileParam.ioDirID = spec->parID;
  444.         pb.fileParam.ioNamePtr = (StringPtr) &(spec->name);
  445.         pb.fileParam.ioFVersNum = 0;
  446.         pb.fileParam.ioFDirIndex = 0;
  447.         result = PBHGetFInfoSync(&pb);
  448.         *fndrInfo = pb.fileParam.ioFlFndrInfo;
  449.         return ( result );
  450.     }
  451.     else
  452. #endif    /* !__MACOSSEVENORLATER */
  453.     {
  454.         return ( FSpGetFInfo(spec, fndrInfo) );
  455.     }
  456. }
  457.  
  458. /*****************************************************************************/
  459.  
  460. pascal    OSErr    FSpSetFInfoCompat(const FSSpec *spec,
  461.                                   const FInfo *fndrInfo)
  462. {
  463. #if !__MACOSSEVENORLATER
  464.     if ( !FSHasFSSpecCalls() && !QTHasFSSpecCalls() )
  465.     {
  466.         OSErr            result;
  467.         HParamBlockRec    pb;
  468.         
  469.         pb.fileParam.ioVRefNum = spec->vRefNum;
  470.         pb.fileParam.ioDirID = spec->parID;
  471.         pb.fileParam.ioNamePtr = (StringPtr) &(spec->name);
  472.         pb.fileParam.ioFVersNum = 0;
  473.         pb.fileParam.ioFDirIndex = 0;
  474.         result = PBHGetFInfoSync(&pb);
  475.         if ( result == noErr )
  476.         {
  477.             pb.fileParam.ioFlFndrInfo = *fndrInfo;
  478.             pb.fileParam.ioDirID = spec->parID;
  479.             result = PBHSetFInfoSync(&pb);
  480.         }
  481.         return ( result );
  482.     }
  483.     else
  484. #endif    /* !__MACOSSEVENORLATER */
  485.     {
  486.         return ( FSpSetFInfo(spec, fndrInfo) );
  487.     }
  488. }
  489.  
  490. /*****************************************************************************/
  491.  
  492. pascal    OSErr    FSpSetFLockCompat(const FSSpec *spec)
  493. {
  494. #if !__MACOSSEVENORLATER
  495.     if ( !FSHasFSSpecCalls() && !QTHasFSSpecCalls() )
  496.     {
  497.         HParamBlockRec    pb;
  498.         
  499.         pb.fileParam.ioVRefNum = spec->vRefNum;
  500.         pb.fileParam.ioDirID = spec->parID;
  501.         pb.fileParam.ioNamePtr = (StringPtr) &(spec->name);
  502.         pb.fileParam.ioFVersNum = 0;
  503.         return ( PBHSetFLockSync(&pb) );
  504.     }
  505.     else
  506. #endif    /* !__MACOSSEVENORLATER */
  507.     {
  508.         return ( FSpSetFLock(spec) );
  509.     }
  510. }
  511.  
  512. /*****************************************************************************/
  513.  
  514. pascal    OSErr    FSpRstFLockCompat(const FSSpec *spec)
  515. {
  516. #if !__MACOSSEVENORLATER
  517.     if ( !FSHasFSSpecCalls() && !QTHasFSSpecCalls() )
  518.     {
  519.         HParamBlockRec    pb;
  520.         
  521.         pb.fileParam.ioVRefNum = spec->vRefNum;
  522.         pb.fileParam.ioDirID = spec->parID;
  523.         pb.fileParam.ioNamePtr = (StringPtr) &(spec->name);
  524.         pb.fileParam.ioFVersNum = 0;
  525.         return ( PBHRstFLockSync(&pb) );
  526.     }
  527.     else
  528. #endif    /* !__MACOSSEVENORLATER */
  529.     {
  530.         return ( FSpRstFLock(spec) );
  531.     }
  532. }
  533.  
  534. /*****************************************************************************/
  535.  
  536. pascal    OSErr    FSpRenameCompat(const FSSpec *spec,
  537.                                 ConstStr255Param newName)
  538. {
  539. #if !__MACOSSEVENORLATER
  540.     if ( !FSHasFSSpecCalls() && !QTHasFSSpecCalls() )
  541.     {
  542.         HParamBlockRec    pb;
  543.         
  544.         pb.ioParam.ioVRefNum = spec->vRefNum;
  545.         pb.fileParam.ioDirID = spec->parID;
  546.         pb.ioParam.ioNamePtr = (StringPtr) &(spec->name);
  547.         pb.ioParam.ioVersNum = 0;
  548.         pb.ioParam.ioMisc = (Ptr) newName;
  549.         return ( PBHRenameSync(&pb) );
  550.     }
  551.     else
  552. #endif    /* !__MACOSSEVENORLATER */
  553.     {
  554.         return ( FSpRename(spec, newName) );
  555.     }
  556. }
  557.  
  558. /*****************************************************************************/
  559.  
  560. pascal    OSErr    FSpCatMoveCompat(const FSSpec *source,
  561.                                  const FSSpec *dest)
  562. {
  563. #if !__MACOSSEVENORLATER
  564.     if ( !FSHasFSSpecCalls() && !QTHasFSSpecCalls() )
  565.     {
  566.         CMovePBRec    pb;
  567.         
  568.         /* source and destination volume must be the same */
  569.         if ( source->vRefNum != dest->vRefNum )
  570.             return ( paramErr );
  571.         
  572.         pb.ioNamePtr = (StringPtr) &(source->name);
  573.         pb.ioVRefNum = source->vRefNum;
  574.         pb.ioDirID = source->parID;
  575.         pb.ioNewDirID = dest->parID;
  576.         pb.ioNewName = (StringPtr) &(dest->name);
  577.         return ( PBCatMoveSync(&pb) );
  578.     }
  579.     else
  580. #endif    /* !__MACOSSEVENORLATER */
  581.     {
  582.         return ( FSpCatMove(source, dest) );
  583.     }
  584. }
  585.  
  586. /*****************************************************************************/
  587.  
  588. /* GenerateUniqueName generates a name that is unique in both dir1 and dir2 */
  589. /* on the specified volume. Ripped off from Feldman's code. */
  590.  
  591. #if !__MACOSSEVENFIVEORLATER
  592. static    OSErr    GenerateUniqueName(short volume,
  593.                                    long *startSeed,
  594.                                    long dir1,
  595.                                    long dir2,
  596.                                    StringPtr uniqueName)
  597. {
  598.     OSErr            error = noErr;
  599.     long            i;
  600.     CInfoPBRec        cinfo;
  601.     unsigned char    hexStr[16];
  602.     
  603.     for ( i = 0; i < 16; ++i )
  604.     {
  605.         if ( i < 10 )
  606.         {
  607.             hexStr[i] = 0x30 + i;
  608.         }
  609.         else
  610.         {
  611.             hexStr[i] = 0x37 + i;
  612.         }
  613.     }
  614.     
  615.     cinfo.hFileInfo.ioVRefNum = volume;
  616.     cinfo.hFileInfo.ioFDirIndex = 0;
  617.     cinfo.hFileInfo.ioNamePtr = uniqueName;
  618.  
  619.     while ( error != fnfErr )
  620.     {
  621.         (*startSeed)++;        
  622.         cinfo.hFileInfo.ioNamePtr[0] = 8;
  623.         for ( i = 1; i <= 8; i++ )
  624.         {
  625.             cinfo.hFileInfo.ioNamePtr[i] = hexStr[((*startSeed >> ((8-i)*4)) & 0xf)];
  626.         }
  627.         cinfo.hFileInfo.ioDirID = dir1;
  628.         error = fnfErr;
  629.         for ( i = 1; i <= 2; i++ )
  630.         {
  631.             error = error & PBGetCatInfoSync(&cinfo);
  632.             cinfo.hFileInfo.ioDirID = dir2;
  633.             if ( (error != fnfErr) && (error != noErr) )
  634.             {
  635.                 return ( error );
  636.             }
  637.         }
  638.     }
  639.     return ( noErr );
  640. }
  641. #endif    /* !__MACOSSEVENFIVEORLATER */
  642.  
  643. /*****************************************************************************/
  644.  
  645. pascal    OSErr    FSpExchangeFilesCompat(const FSSpec *source,
  646.                                        const FSSpec *dest)
  647. {
  648. #if !__MACOSSEVENFIVEORLATER
  649.     if ( 
  650. #if !__MACOSSEVENORLATER
  651.          !FSHasFSSpecCalls() ||
  652. #endif    /* !__MACOSSEVENORLATER */
  653.          !HasFSpExchangeFilesCompatibilityFix() )
  654.     {
  655.         HParamBlockRec            pb;
  656.         CInfoPBRec                catInfoSource, catInfoDest;
  657.         OSErr                    result, result2;
  658.         Str31                    unique1, unique2;
  659.         StringPtr                unique1Ptr, unique2Ptr, swapola;
  660.         GetVolParmsInfoBuffer    volInfo;
  661.         long                    theSeed, temp;
  662.         
  663.         /* Make sure the source and destination are on the same volume */
  664.         if ( source->vRefNum != dest->vRefNum )
  665.         {
  666.             result = diffVolErr;
  667.             goto errorExit3;
  668.         }
  669.         
  670.         /* Try PBExchangeFiles first since it preserves the file ID reference */
  671.         pb.fidParam.ioNamePtr = (StringPtr) &(source->name);
  672.         pb.fidParam.ioVRefNum = source->vRefNum;
  673.         pb.fidParam.ioDestNamePtr = (StringPtr) &(dest->name);
  674.         pb.fidParam.ioDestDirID = dest->parID;
  675.         pb.fidParam.ioSrcDirID = source->parID;
  676.     
  677.         result = PBExchangeFilesSync(&pb);
  678.     
  679.         /* Note: The compatibility case won't work for files with *Btree control blocks. */
  680.         /* Right now the only *Btree files are created by the system. */
  681.         if ( result != noErr )
  682.         {
  683.             pb.ioParam.ioNamePtr = NULL;
  684.             pb.ioParam.ioBuffer = (Ptr) &volInfo;
  685.             pb.ioParam.ioReqCount = sizeof(volInfo);
  686.             result2 = PBHGetVolParmsSync(&pb);
  687.             
  688.             /* continue if volume has no fileID support (or no GetVolParms support) */
  689.             if ( (result2 == noErr) && hasFileIDs(volInfo) )
  690.             {
  691.                 goto errorExit3;
  692.             }
  693.     
  694.             /* Get the catalog information for each file */
  695.             /* and make sure both files are *really* files */
  696.             catInfoSource.hFileInfo.ioVRefNum = source->vRefNum;
  697.             catInfoSource.hFileInfo.ioFDirIndex = 0;
  698.             catInfoSource.hFileInfo.ioNamePtr = (StringPtr) &(source->name);
  699.             catInfoSource.hFileInfo.ioDirID = source->parID;
  700.             catInfoSource.hFileInfo.ioACUser = 0; /* ioACUser used to be filler2 */
  701.             result = PBGetCatInfoSync(&catInfoSource);
  702.             if ( result != noErr )
  703.             {
  704.                 goto errorExit3;
  705.             }
  706.             if ( (catInfoSource.hFileInfo.ioFlAttrib & ioDirMask) != 0 )
  707.             {
  708.                 result = notAFileErr;
  709.                 goto errorExit3;
  710.             }
  711.             
  712.             catInfoDest.hFileInfo.ioVRefNum = dest->vRefNum;
  713.             catInfoDest.hFileInfo.ioFDirIndex = 0;
  714.             catInfoDest.hFileInfo.ioNamePtr = (StringPtr) &(dest->name);
  715.             catInfoDest.hFileInfo.ioDirID = dest->parID;
  716.             catInfoDest.hFileInfo.ioACUser = 0; /* ioACUser used to be filler2 */
  717.             result = PBGetCatInfoSync(&catInfoDest);
  718.             if ( result != noErr )
  719.             {
  720.                 goto errorExit3;
  721.             }
  722.             if ( (catInfoDest.hFileInfo.ioFlAttrib & ioDirMask) != 0 )
  723.             {
  724.                 result = notAFileErr;
  725.                 goto errorExit3;
  726.             }
  727.             
  728.             /* generate 2 filenames that are unique in both directories */
  729.             theSeed = 0x64666A6C;    /* a fine unlikely filename */
  730.             unique1Ptr = (StringPtr)&unique1;
  731.             unique2Ptr = (StringPtr)&unique2;
  732.             
  733.             result = GenerateUniqueName(source->vRefNum, &theSeed, source->parID, dest->parID, unique1Ptr);
  734.             if ( result != noErr )
  735.             {
  736.                 goto errorExit3;
  737.             }
  738.     
  739.             GenerateUniqueName(source->vRefNum, &theSeed, source->parID, dest->parID, unique2Ptr);
  740.             if ( result != noErr )
  741.             {
  742.                 goto errorExit3;
  743.             }
  744.     
  745.             /* rename source to unique1 */
  746.             pb.fileParam.ioNamePtr = (StringPtr) &(source->name);
  747.             pb.ioParam.ioMisc = (Ptr) unique1Ptr;
  748.             pb.ioParam.ioVersNum = 0;
  749.             result = PBHRenameSync(&pb);
  750.             if ( result != noErr )
  751.             {
  752.                 goto errorExit3;
  753.             }
  754.             
  755.             /* rename dest to unique2 */
  756.             pb.ioParam.ioMisc = (Ptr) unique2Ptr;
  757.             pb.ioParam.ioVersNum = 0;
  758.             pb.fileParam.ioNamePtr = (StringPtr) &(dest->name);
  759.             pb.fileParam.ioDirID = dest->parID;
  760.             result = PBHRenameSync(&pb);
  761.             if ( result != noErr )
  762.             {
  763.                 goto errorExit2;    /* back out gracefully by renaming unique1 back to source */
  764.             }
  765.                 
  766.             /* If files are not in same directory, swap their locations */
  767.             if ( source->parID != dest->parID )
  768.             {
  769.                 /* move source file to dest directory */
  770.                 pb.copyParam.ioNamePtr = unique1Ptr;
  771.                 pb.copyParam.ioNewName = NULL;
  772.                 pb.copyParam.ioNewDirID = dest->parID;
  773.                 pb.copyParam.ioDirID = source->parID;
  774.                 result = PBCatMoveSync((CMovePBPtr) &pb);
  775.                 if ( result != noErr )
  776.                 {
  777.                     goto errorExit1;    /* back out gracefully by renaming both files to original names */
  778.                 }
  779.                 
  780.                 /* move dest file to source directory */
  781.                 pb.copyParam.ioNamePtr = unique2Ptr;
  782.                 pb.copyParam.ioNewDirID = source->parID;
  783.                 pb.copyParam.ioDirID = dest->parID;
  784.                 result = PBCatMoveSync((CMovePBPtr) &pb);
  785.                 if ( result != noErr)
  786.                 {
  787.                     /* life is very bad.  We'll at least try to move source back */
  788.                     pb.copyParam.ioNamePtr = unique1Ptr;
  789.                     pb.copyParam.ioNewName = NULL;
  790.                     pb.copyParam.ioNewDirID = source->parID;
  791.                     pb.copyParam.ioDirID = dest->parID;
  792.                     (void) PBCatMoveSync((CMovePBPtr) &pb);    /* ignore errors */
  793.                     goto errorExit1;    /* back out gracefully by renaming both files to original names */
  794.                 }
  795.             }
  796.             
  797.             /* Make unique1Ptr point to file in source->parID */
  798.             /* and unique2Ptr point to file in dest->parID */
  799.             /* This lets us fall through to the rename code below */
  800.             swapola = unique1Ptr;
  801.             unique1Ptr = unique2Ptr;
  802.             unique2Ptr = swapola;
  803.     
  804.             /* At this point, the files are in their new locations (if they were moved) */
  805.             /* Source is named Unique1 (name pointed to by unique2Ptr) and is in dest->parID */
  806.             /* Dest is named Unique2 (name pointed to by unique1Ptr) and is in source->parID */
  807.             /* Need to swap attributes except mod date and swap names */
  808.     
  809.             /* swap the catalog info by re-aiming the CInfoPB's */
  810.             catInfoSource.hFileInfo.ioNamePtr = unique1Ptr;
  811.             catInfoDest.hFileInfo.ioNamePtr = unique2Ptr;
  812.             
  813.             catInfoSource.hFileInfo.ioDirID = source->parID;
  814.             catInfoDest.hFileInfo.ioDirID = dest->parID;
  815.             
  816.             /* Swap the original mod dates with each file */
  817.             temp = catInfoSource.hFileInfo.ioFlMdDat;
  818.             catInfoSource.hFileInfo.ioFlMdDat = catInfoDest.hFileInfo.ioFlMdDat;
  819.             catInfoDest.hFileInfo.ioFlMdDat = temp;
  820.             
  821.             /* Here's the swap (ignore errors) */
  822.             (void) PBSetCatInfoSync(&catInfoSource); 
  823.             (void) PBSetCatInfoSync(&catInfoDest);
  824.             
  825.             /* rename unique2 back to dest */
  826. errorExit1:
  827.             pb.ioParam.ioMisc = (Ptr) &(dest->name);
  828.             pb.ioParam.ioVersNum = 0;
  829.             pb.fileParam.ioNamePtr = unique2Ptr;
  830.             pb.fileParam.ioDirID = dest->parID;
  831.             (void) PBHRenameSync(&pb);    /* ignore errors */
  832.     
  833.             /* rename unique1 back to source */
  834. errorExit2:
  835.             pb.ioParam.ioMisc = (Ptr) &(source->name);
  836.             pb.ioParam.ioVersNum = 0;
  837.             pb.fileParam.ioNamePtr = unique1Ptr;
  838.             pb.fileParam.ioDirID = source->parID;
  839.             (void) PBHRenameSync(&pb); /* ignore errors */
  840.         }
  841. errorExit3: { /* null statement */ }
  842.         return ( result );
  843.     }
  844.     else
  845. #endif    /* !__MACOSSEVENFIVEORLATER */
  846.     {
  847.         return ( FSpExchangeFiles(source, dest) );
  848.     }
  849. }
  850.  
  851. /*****************************************************************************/
  852.  
  853. /* 
  854. **    Resource Manager FSp calls
  855. */
  856.  
  857. /*****************************************************************************/
  858.  
  859. pascal    short    FSpOpenResFileCompat(const FSSpec *spec,
  860.                                      SignedByte permission)
  861. {
  862. #if !__MACOSSEVENORLATER
  863.     if ( !FSHasFSSpecCalls() && !QTHasFSSpecCalls() )
  864.     {
  865.         return ( HOpenResFile(spec->vRefNum, spec->parID, spec->name, permission) );
  866.     }
  867.     else
  868. #endif    /* !__MACOSSEVENORLATER */
  869.     {
  870.         return ( FSpOpenResFile(spec, permission) );
  871.     }
  872. }
  873.  
  874. /*****************************************************************************/
  875.  
  876. pascal    void    FSpCreateResFileCompat(const FSSpec *spec,
  877.                                        OSType creator,
  878.                                        OSType fileType,
  879.                                        ScriptCode scriptTag)
  880. {    
  881. #if !__MACOSSEVENFIVEONEORLATER
  882.     if (
  883. #if !__MACOSSEVENORLATER
  884.          (!FSHasFSSpecCalls() && !QTHasFSSpecCalls()) ||
  885. #endif    /* !__MACOSSEVENORLATER */
  886.          !HasFSpCreateScriptSupportFix() )
  887.     {
  888.         OSErr            result;
  889.         CInfoPBRec        pb;
  890.         
  891.         HCreateResFile(spec->vRefNum, spec->parID, spec->name);
  892.         if ( ResError() == noErr )
  893.         {
  894.             /* get info on created item */
  895.             pb.hFileInfo.ioVRefNum = spec->vRefNum;
  896.             pb.hFileInfo.ioDirID = spec->parID;
  897.             pb.hFileInfo.ioNamePtr = (StringPtr) &(spec->name);
  898.             pb.hFileInfo.ioFDirIndex = 0;
  899.             result = PBGetCatInfoSync(&pb);
  900.             if ( result == noErr )
  901.             {
  902.                 /* Set fdScript in FXInfo */
  903.                 /* The negative script constants (smSystemScript, smCurrentScript, and smAllScripts) */
  904.                 /* don't make sense on disk, so only use scriptTag if scriptTag >= smRoman */
  905.                 /* (smRoman is 0). fdScript is valid if high bit is set (see IM-6, page 9-38) */
  906.                 pb.hFileInfo.ioFlXFndrInfo.fdScript = (scriptTag >= smRoman) ?
  907.                                                         ((char)scriptTag | (char)0x80) :
  908.                                                         (smRoman);
  909.                 /* Set creator/fileType */
  910.                 pb.hFileInfo.ioFlFndrInfo.fdCreator = creator;
  911.                 pb.hFileInfo.ioFlFndrInfo.fdType = fileType;
  912.                 
  913.                 /* Restore ioDirID field in pb which was changed by PBGetCatInfo */
  914.                 pb.hFileInfo.ioDirID = spec->parID;
  915.                 result = PBSetCatInfoSync(&pb);
  916.             }
  917.             /* Set ResErr low memory global to result */
  918.             LMSetResErr(result);
  919.         }
  920.         return;
  921.     }
  922.     else
  923. #endif    /* !__MACOSSEVENFIVEONEORLATER */
  924.     {
  925.         FSpCreateResFile(spec, creator, fileType, scriptTag);
  926.         return;
  927.     }
  928. }
  929.  
  930. /*****************************************************************************/
  931.